﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///JYZX 的摘要说明
/// </summary>
public   partial class JYZX
{
    public enum type { 如何买书及付款 = 1, 选课, 个人辅导, 培训, 学付宝}
    public static JYZX GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.JYZX.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<JYZX> GetAllByConditon(int Type, string AddTimeForm, string AddTimeTo, int AddUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.JYZX
                        select t;
            // 建议类型
            if (Type != 0)
                query = query.Where(row => row.Type == Type);
            // 添加时间
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            // 添加用户
            if (AddUserID != 0)
                query = query.Where(row => row.AddUserID == AddUserID);
            query = query.OrderByDescending(row => row.AddTime);
            return query.ToList();
        }
    }

    public static List<JYZX> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.JYZX.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.JYZX.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<JYZX>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.JYZX.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.JYZX.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
}